import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;

import java.util.ArrayList;
import java.util.List;

public class ContinuousChatClient {

    // 用于保存对话历史
    private static List<Message> conversationHistory = new ArrayList<>();

    public static void main(String[] args) {
        try {
            // 第一次对话
            sendMessage("我叫邓旭阳");

            // 第二次对话
            sendMessage("我叫什么？");


        } catch (Exception e) {
            System.out.println("请求出错：" + e.getMessage());
        }
    }

    // 发送消息的函数
    private static void sendMessage(String userMessage) {
        // 将用户消息添加到对话历史
        conversationHistory.add(new Message("user", userMessage));

        // 创建对话消息体
        StringBuilder messagesBody = new StringBuilder();
        for (Message message : conversationHistory) {
            messagesBody.append(String.format("{\"role\": \"%s\", \"content\": \"%s\"},\n", message.getRole(), message.getContent()));
        }

        // 去掉最后一个逗号
        if (messagesBody.length() > 0) {
            messagesBody.setLength(messagesBody.length() - 2);
        }

        // 发送请求
        HttpResponse<String> response = Unirest.post("https://api.siliconflow.cn/v1/chat/completions")
                .header("Authorization", "Bearer sk-dnuenljimzbsgblokugqnrhbplplbrllkltqkpaszcnjsuuf")
                .header("Content-Type", "application/json")
                .body("{\n" +
                      "  \"model\": \"Pro/deepseek-ai/DeepSeek-V3\",\n" +
                      "  \"stream\": false,\n" +
                      "  \"max_tokens\": 512,\n" +
                      "  \"temperature\": 0.7,\n" +
                      "  \"top_p\": 0.7,\n" +
                      "  \"top_k\": 50,\n" +
                      "  \"frequency_penalty\": 0.5,\n" +
                      "  \"n\": 1,\n" +
                      "  \"messages\": [" + messagesBody + "]\n" +
                      "}")
                .asString();

        // 输出响应状态码
        System.out.println("Response Status: " + response.getStatus());

        // 获取并打印响应的 JSON 内容
        String responseBody = response.getBody();
        System.out.println("Response Body: " + responseBody);

        // 解析 JSON 响应
        JsonNode jsonResponse = new JsonNode(responseBody);

        // 提取 AI 回复内容
        String aiReply = jsonResponse.getObject()
                .getJSONArray("choices")
                .getJSONObject(0)
                .getJSONObject("message")
                .getString("content");

        // 打印 AI 的回复
        System.out.println("Generated Response: " + aiReply);

        // 将 AI 的回复添加到对话历史
        conversationHistory.add(new Message("assistant", aiReply));
    }

    // 内部类，用于存储每条消息的角色和内容
    static class Message {
        private String role;
        private String content;

        public Message(String role, String content) {
            this.role = role;
            this.content = content;
        }

        public String getRole() {
            return role;
        }

        public String getContent() {
            return content;
        }
    }
}
